package com.byl.common.modules.controller;

import com.byl.sdk.bean.Response;
import com.byl.sdk.bean.ResponsePage;
import com.byl.sdk.bean.ResultBean;
import com.byl.sdk.gson.GsonUtils;

/**
 * Created by Administrator on 2019/7/15.
 */
public abstract class BaseController {
    private static final int SUCCESS_CODE = 200;  //成功
    private static final int FAIL_CODE = 0; //错误
    /**
     * 处理成功
     *
     * @param msg 成功消息
     * @param obj 成功数据
     * @return 返回json字符串
     */
    protected String success(String msg, Object obj) {
        return GsonUtils.beanToJson(new Response(SUCCESS_CODE, msg, obj));
    }
    /**
     * 处理成功
     *
     * @param msg 成功消息
     * @return 返回json字符串
     */
    protected String success(String msg) {
        return GsonUtils.beanToJson(new Response(SUCCESS_CODE, msg));
    }

    /**
     * 分页结果封装
     *
     * @param msg
     * @param bean
     * @return
     */
    protected String success(String msg, ResultBean bean) {
        return GsonUtils.beanToJson(new ResponsePage(SUCCESS_CODE, msg, bean.getData(), bean.getPage(), bean.getPageSize(), bean.getTotal()));
    }

    /**
     * 处理失败
     *
     * @param msg 错误消息
     * @return 返回的json字符串
     */
    protected String fail(String msg) {
        return GsonUtils.beanToJson(new Response(FAIL_CODE, msg));
    }

    /**
     * 处理失败
     *
     * @param msg 错误消息
     * @param obj 错误数据或者提示错误信息
     * @return 返回json字符串
     */
    protected String fail(String msg, Object obj) {
        return GsonUtils.beanToJson(new Response(FAIL_CODE, msg, obj));
    }
}
